import re


# 读取文件
def read_logs(path):
    with open(path, mode="r", encoding="UTF-8") as file:
        return [line.rstrip() for line in file.readlines()]


# 解析日志
def decode_log(logs):
    # 保存解析结果
    logs_list = []
    for log in logs:
        # 日志类型
        log_type = re.search("\[([A-Z]{4,5})]", log).group(1)

        # 日期打印的时间
        log_time = re.search("\d{4}.+?\d{3}", log).group()

        # 类
        log_class = re.search("0000 (.+?):", log).group(1)

        # 日志描述
        log_desc = re.search("- (.+)", log).group(1)

        logs_list.append((log_type, log_time, log_class, log_desc))

    # 返回结果
    return logs_list


# 保存日志文件
def log_to_file(logs_list, path):
    with open(path, mode="w", encoding="UTF-8") as file:
        for log_type, log_time, log_class, log_desc in logs_list:
            file.write(f"{log_type}\t{log_time}\t{log_class}\t{log_desc}\n")


# 通用的用于统计列表中每个元素个数的函数
def reduce(list):
    sum_dict = {}

    for value in list:
        if value in sum_dict:
            sum_dict[value] = sum_dict[value] + 1
        else:
            sum_dict[value] = 1

    # 返回结果
    return sum_dict


# 将类型统计的结果保存到文件
def result_to_file(dict, path):
    with open(path, mode="w", encoding="UTF-8") as file:
        for key, value in dict.items():
            file.write(f"{key},{value}\n")


# 解析单词
def decode_words(logs_list):
    # 取出描述
    descs = [log[-1] for log in logs_list]

    words = []

    for desc in descs:
        # 查找一行中的多个单词
        desc_list = re.findall("[a-zA-Z][\w]+", desc)

        for word in desc_list:
            # 将单词一个一个放到words中
            words.append(word)

    return words


if __name__ == '__main__':
    # 1、读取日志文件
    logs = read_logs("../../data/log.log")

    # 2、解析日志文件
    logs_list = decode_log(logs)

    # 3、保存结果到文件
    log_to_file(logs_list, "../../data/decode_log.log")

    ######################4、统计类型的数量###########################
    # 取出类型
    logs_types = [log[0] for log in logs_list]
    # 4、统计类型的数量
    log_type_num = reduce(logs_types)
    # 将结果保存到文件
    result_to_file(log_type_num, "../../data/log_type_num.txt")
    print(log_type_num)

    ######################4、统计每一天日志的数量###########################
    logs_date = [log[1][0:10] for log in logs_list]
    # 统计日期的数量
    log_date_num = reduce(logs_date)
    # 保存到文件
    result_to_file(log_date_num, "../../data/log_date_num.txt")
    print(log_date_num)

    ######################单词出现的数量###########################
    # 解析单词
    words = decode_words(logs_list)
    # 计算单词的数量
    word_num = reduce(words)
    # 保存结果
    result_to_file(word_num, "../../data/word_num.txt")
